// const list = [1, 3, 5, 6, 3, 2, 3, 4]
// function fn (list) {
//     const arr = []
//     for (let i = 0; i < list.length; i++) {
//         for (let k = i + 1; k < list.length; k++) {
//             if (list[i] == list[k]) { } else {
//                 arr.push(list[i])
//             }

//         }
//     }
//     return arr
// }
// console.log(fn(list))


// const str = 'dsfklasjkflajl'
// function fn (str) {
//     const list = []
//     for (let i = 0; i < str.length; i++) {
//         list.unshift(str[i])
//     }
//     return list.join('')
// }
// console.log(fn(str))

const map = new Map()
map.set('a', 'a')
map.set('cc', 'cc')
const data = {
    name: 1,
    say () { },
    time: new Date(),
    list: [1, 2, { age: 12 }, [new Set(), 44, 55, {}]],
    sy: Symbol(1),
    map: map,
    no: undefined,
    obj: {
        name: '',
        age: 0,
        type: undefined
    }
}

console.log()
function deepClone (data) {
    let cloneData = Array.isArray(data) ? [] : {}
    if (typeof data === 'object' && data !== null) {
        for (const k in data) {
            if (Object.hasOwn(data, k)) {
                if (Object.prototype.toString.call(data).slice(8, -1).toLowerCase() === 'date') {
                    cloneData[k] = new Date(data[k].getTime())
                } else {
                    cloneData[k] = deepClone(data[k])
                }

            }
        }
    } else {
        cloneData = data
    }
    return cloneData
}

console.log(deepClone(data))


// async function deepClone (data) {
//     const newObj = null
//     const promise = new Promise((res, rej) => {
//         const { port1, port2 } = new MessageChannel()
//         port1.postMessage(data)
//         port2.onmessage = e => {
//             res(e)
//         }
//     })
//     await promise.then(res => {
//         newObj = res
//     })
//     return newObj
// }
// console.log(structuredClone(data));




function deepClone1 (data) {
    let cloneData
    if (typeof data === 'object' && data !== null) {
        cloneData = data instanceof Array ? [] : {}
        for (const k in data) {
            if (Object.hasOwnProperty.call(data, k)) {
                if (Object.prototype.toString.call(data).slice(8, -1).toLowerCase() === 'date') {
                    cloneData[k] = new Date(data[k].getTime())
                } else {
                    cloneData[k] = deepClone1(data[k])
                }
            }
        }
    } else {
        cloneData = data
    }
    return cloneData
}
console.log(deepClone1(data));

